#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution
{
public:
    void sortColors(vector<int>& nums)
    {
        int left = -1;
        int cur = 0;
        int right = nums.size();

        while(cur < right)
        {
            if(nums[cur]==0)
            {
                swap(nums[cur++], nums[++left]);
            }
            else if(nums[cur]==1)
            {
                cur++;
            }
            else
            {
                swap(nums[cur], nums[--right]);
            }
        }
    }
};